package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallGroupon;
import org.linlinjava.litemall.db.mapper.LitemallGrouponDao;
import org.linlinjava.litemall.db.util.GrouponConstant;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;

@Service
public class LitemallGrouponService {
    @Resource
    private LitemallGrouponDao mapper;

    /**
     * 获取用户发起的团购记录
     *
     * @param userId
     * @return
     */
    public List<LitemallGroupon> queryMyGroupon(Integer userId) {
    	
    	LambdaQuery<LitemallGroupon> query = mapper.createLambdaQuery();
    	query.andEq(LitemallGroupon::getUserId, userId);
    	query.andEq(LitemallGroupon::getCreatorUserId, userId);
    	query.andEq(LitemallGroupon::getGrouponId, 0);
    	query.andNotEq(LitemallGroupon::getStatus, GrouponConstant.STATUS_NONE);
    	query.andEq(LitemallGroupon::getDeleted, false);
    	query.orderBy("add_time desc");
    	return query.select();
		/*
		 * LitemallGrouponExample example = new LitemallGrouponExample();
		 * example.or().andUserIdEqualTo(userId).andCreatorUserIdEqualTo(userId).
		 * andGrouponIdEqualTo(0).andStatusNotEqualTo(GrouponConstant.STATUS_NONE).
		 * andDeletedEqualTo(false); example.orderBy("add_time desc"); return
		 * mapper.selectByExample(example);
		 */
    }

    /**
     * 获取用户参与的团购记录
     *
     * @param userId
     * @return
     */
    public List<LitemallGroupon> queryMyJoinGroupon(Integer userId) {
    	
    	LambdaQuery<LitemallGroupon> query = mapper.createLambdaQuery();
    	query.andEq(LitemallGroupon::getUserId, userId);
    	query.andEq(LitemallGroupon::getGrouponId, 0);
    	query.andNotEq(LitemallGroupon::getStatus, GrouponConstant.STATUS_NONE);
    	query.andEq(LitemallGroupon::getDeleted, false);
    	query.orderBy("add_time desc");
    	return query.select();
    	
    	
		/*
		 * LitemallGrouponExample example = new LitemallGrouponExample();
		 * example.or().andUserIdEqualTo(userId).andGrouponIdNotEqualTo(0).
		 * andStatusNotEqualTo(GrouponConstant.STATUS_NONE).andDeletedEqualTo(false);
		 * example.orderBy("add_time desc"); return mapper.selectByExample(example);
		 */
    }

    /**
     * 根据OrderId查询团购记录
     *
     * @param orderId
     * @return
     */
    public LitemallGroupon queryByOrderId(Integer orderId) {
    	
    	LitemallGroupon query = new LitemallGroupon();
    	query.setOrderId(orderId);
    	query.setDeleted(false);
    	return mapper.templateOne(query);
		/*
		 * 
		 * LitemallGrouponExample example = new LitemallGrouponExample();
		 * example.or().andOrderIdEqualTo(orderId).andDeletedEqualTo(false); return
		 * mapper.selectOneByExample(example);
		 */
    }

    /**
     * 获取某个团购活动参与的记录
     *
     * @param id
     * @return
     */
    public List<LitemallGroupon> queryJoinRecord(Integer id) {
    	
    	LambdaQuery<LitemallGroupon> query = mapper.createLambdaQuery();
    	query.andEq(LitemallGroupon::getGrouponId, id);
    	query.andNotEq(LitemallGroupon::getStatus, GrouponConstant.STATUS_NONE);
    	query.andEq(LitemallGroupon::getDeleted, false);
    	query.orderBy("add_time desc");
    	return query.select();
    	
    	
		/*
		 * LitemallGrouponExample example = new LitemallGrouponExample();
		 * example.or().andGrouponIdEqualTo(id).andStatusNotEqualTo(GrouponConstant.
		 * STATUS_NONE).andDeletedEqualTo(false); example.orderBy("add_time desc");
		 * return mapper.selectByExample(example);
		 */
    }

    /**
     * 根据ID查询记录
     *
     * @param id
     * @return
     */
    public LitemallGroupon queryById(Integer id) {
    	LitemallGroupon query = new LitemallGroupon();
    	query.setId(id);
    	query.setDeleted(false);
    	return mapper.templateOne(query);
		/*
		 * LitemallGrouponExample example = new LitemallGrouponExample();
		 * example.or().andIdEqualTo(id).andDeletedEqualTo(false); return
		 * mapper.selectOneByExample(example);
		 */
    }

    /**
     * 根据ID查询记录
     *
     * @param userId
     * @param id
     * @return
     */
    public LitemallGroupon queryById(Integer userId, Integer id) {
    	
    	LitemallGroupon query = new LitemallGroupon();
    	query.setId(id);
    	query.setUserId(userId);
    	query.setDeleted(false);
    	return mapper.templateOne(query);
		/*
		 * LitemallGrouponExample example = new LitemallGrouponExample();
		 * example.or().andIdEqualTo(id).andUserIdEqualTo(userId).andDeletedEqualTo(
		 * false); return mapper.selectOneByExample(example);
		 */
    }

    /**
     * 返回某个发起的团购参与人数
     *
     * @param grouponId
     * @return
     */
    public int countGroupon(Integer grouponId) {
    	

    	LambdaQuery<LitemallGroupon> query = mapper.createLambdaQuery();
    	query.andEq(LitemallGroupon::getGrouponId, grouponId);
    	query.andNotEq(LitemallGroupon::getStatus, GrouponConstant.STATUS_NONE);
    	query.andEq(LitemallGroupon::getDeleted, false);
    	return (int)query.count();
    	
		/*
		 * LitemallGrouponExample example = new LitemallGrouponExample();
		 * example.or().andGrouponIdEqualTo(grouponId).andStatusNotEqualTo(
		 * GrouponConstant.STATUS_NONE).andDeletedEqualTo(false); return (int)
		 * mapper.countByExample(example);
		 */
    }

    public boolean hasJoin(Integer userId, Integer grouponId) {
    	
    	LambdaQuery<LitemallGroupon> query = mapper.createLambdaQuery();
    	query.andEq(LitemallGroupon::getUserId, userId);
    	query.andEq(LitemallGroupon::getGrouponId, grouponId);
    	query.andNotEq(LitemallGroupon::getStatus, GrouponConstant.STATUS_NONE);
    	query.andEq(LitemallGroupon::getDeleted, false);
    	return  query.count()!=0;
    	
		/*
		 * 
		 * LitemallGrouponExample example = new LitemallGrouponExample();
		 * example.or().andUserIdEqualTo(userId).andGrouponIdEqualTo(grouponId).
		 * andStatusNotEqualTo(GrouponConstant.STATUS_NONE).andDeletedEqualTo(false);
		 * return mapper.countByExample(example) != 0;
		 */
    }

    public int updateById(LitemallGroupon groupon) {
        groupon.setUpdateTime(LocalDateTime.now());
        return mapper.updateTemplateById(groupon);
        
       // return mapper.updateByPrimaryKeySelective(groupon);
    }

    /**
     * 创建或参与一个团购
     *
     * @param groupon
     * @return
     */
    public int createGroupon(LitemallGroupon groupon) {
        groupon.setAddTime(LocalDateTime.now());
        groupon.setUpdateTime(LocalDateTime.now());
         mapper.insertTemplate(groupon,true);
         return 1;
        //return mapper.insertSelective(groupon);
    }


    /**
     * 查询所有发起的团购记录
     *
     * @param rulesId
     * @param page
     * @param size
     * @param sort
     * @param order
     * @return
     */
    public List<LitemallGroupon> querySelective(String rulesId, Integer page, Integer size, String sort, String order) {
    	
    	LambdaQuery<LitemallGroupon> query = mapper.createLambdaQuery();

        if (!StringUtils.isEmpty(rulesId)) {
        	query.andEq(LitemallGroupon::getRulesId, rulesId);
        }
    	
    	query.andEq(LitemallGroupon::getGrouponId, 0);
    	query.andNotEq(LitemallGroupon::getStatus, GrouponConstant.STATUS_NONE);
    	query.andEq(LitemallGroupon::getDeleted, false);
    	query.limit(page, size);
    	return query.select();
    	
		/*
		 * LitemallGrouponExample example = new LitemallGrouponExample();
		 * LitemallGrouponExample.Criteria criteria = example.createCriteria();
		 * 
		 * if (!StringUtils.isEmpty(rulesId)) {
		 * criteria.andRulesIdEqualTo(Integer.parseInt(rulesId)); }
		 * criteria.andDeletedEqualTo(false);
		 * criteria.andStatusNotEqualTo(GrouponConstant.STATUS_NONE);
		 * criteria.andGrouponIdEqualTo(0);
		 * 
		 * PageHelper.startPage(page, size); return mapper.selectByExample(example);
		 */
    }

    public List<LitemallGroupon> queryByRuleId(int grouponRuleId) {
    	
    	LitemallGroupon query = new LitemallGroupon();
    	query.setRulesId(grouponRuleId);
    	query.setDeleted(false);
    	return mapper.template(query);
		/*
		 * LitemallGrouponExample example = new LitemallGrouponExample();
		 * example.or().andRulesIdEqualTo(grouponRuleId).andDeletedEqualTo(false);
		 * return mapper.selectByExample(example);
		 */
    }
}
